package com.sven.player;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sven.player.utils.TimeUtil;
import com.sven.player.widget.PlayButton;
import com.sven.player.widget.WaveLineView;

/**
 * Created by Sven on 2016/10/13 0013.
 */

public class VoicePlayer extends CardView {

    private PlayButton mIv_Play;
    private TextView mTv_Time;
    private WaveLineView waveLineView;
    private LinearLayout cardView;
    private VoiceClickListener voiceClickListener;
    private int mWaveColor;
    private long mDuration;
    Context context;
    private CustomMediaPlayer customMediaPlayer;
    String recordPath;

    public VoicePlayer(Context context) {
        this(context, null);
    }

    public VoicePlayer(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VoicePlayer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttrs(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_player, this);
        this.context = context;
    }

    private void getAttrs(Context context, AttributeSet attrs) {
        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.player);
        mWaveColor = t.getColor(R.styleable.player_wavecolor, 0xffffffff);
        t.recycle();

    }

    public void setWaveColor(int color) {
        waveLineView.setWaveColor(color);
        invalidate();
    }

    public void setBackGroundColor(int color) {
//        cardView.setBackgroundColor(color);
        invalidate();
    }

    public void setPlayBtnBackground(int resId) {
        mIv_Play.setBackgroundResource(resId);
    }

    public void setTimerBackground(int resId) {
        mTv_Time.setBackgroundResource(resId);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mIv_Play = (PlayButton) findViewById(R.id.iv_play);
        mTv_Time = (TextView) findViewById(R.id.tv_time);
        waveLineView = (WaveLineView) findViewById(R.id.waveline);
        cardView = (LinearLayout) findViewById(R.id.cardView);
        waveLineView.setWaveColor(mWaveColor);
        customMediaPlayer = CustomMediaPlayer.getMediaPlayer();
        voiceClickListener = new VoiceClickListener();
        mIv_Play.setOnClickListener(voiceClickListener);
    }

    public String getRecordPath() {
        return recordPath;
    }

    public void setRecordPath(String recordPath) {
        this.recordPath = recordPath;
    }

    public void setData(String recordPath, long ms) {
        play(recordPath, ms);
    }

    @Deprecated
    public void play(String recordPath, long ms) {
        if (customMediaPlayer.getPlaying()) {//判断并且 路径相同
            customMediaPlayer.setWatchOnCompletionListener(watchOnCompletionListener);
        } else {
            this.recordPath = recordPath;
            this.mDuration = ms;
            mTv_Time.setText(TimeUtil.toTimeStr((int) ((mDuration / 1000) + 0.5)));

        }
    }

    private class VoiceClickListener implements OnClickListener {

        public VoiceClickListener() {

        }

        @Override
        public void onClick(View view) {
            try {

                String tempPath = recordPath;
                String currentPath = customMediaPlayer.getCurrentPath();

                if (customMediaPlayer.getPlaying()) {/*之前是播放状态*/
                    if (currentPath == null || tempPath == currentPath) {//如果第二次点击是同一个路径
                        customMediaPlayer.stop();

                    } else {
                        //TODO 重置之前的View 的state 通过 之前设置的setWatchOnCompletionListener 自动完成
                        customMediaPlayer.stop();
                        //TODO 播放现在的
                        toPlay();
                    }
                } else { /*点击之前是未播放状态*/
                    toPlay();
                }
                customMediaPlayer.setCurrentPath(recordPath);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }


    private void toPlay() {


        customMediaPlayer.setWatchOnCompletionListener(watchOnCompletionListener);
        customMediaPlayer.play(recordPath);
    }
    CustomMediaPlayer.WatchOnCompletionListener watchOnCompletionListener = new CustomMediaPlayer.WatchOnCompletionListener() {
        @Override
        public void onCompletion() {
            mIv_Play.setBackgroundResource(R.drawable.ic_play_voice_normal);
            waveLineView.updateAmplitude(0);
            mTv_Time.setText(TimeUtil.toTimeStr((int) ((mDuration / 1000) + 0.5)));

        }

        @Override
        public void onPlaying(PlayingInfo info) {
            mTv_Time.setText(TimeUtil.toTimeStr((int) ((info.getTime() / 1000) + 0.5)));
        }

        @Override
        public void onStart() {
            mIv_Play.setBackgroundResource(R.drawable.ic_stop_voice_normal);
            waveLineView.updateAmplitude(1);
        }

    };
}
